#ifndef XMALLOC_H
#define XMALLOC_H 1

#include "main.h"
#include "log.h"
#include "xmalloc_dbg.h"


#define MEMDBG_STR "MemDbg"

#ifdef PRINT_MEM
# define ncmalloc(n, size)              xmalloc_dbg(n, size, __FILE__, __LINE__)
# define ncrealloc(ptr, size, new_size) xrealloc(ptr, size, new_size); DD("%s: re-alloc(%p,%lu,%lu)", MEMDBG_STR, ptr, size, new_size)
# define ncfree(ptr, size)              xfree(ptr, size); DD("%s: free(%p,%lu)", MEMDBG_STR, ptr, size)
#else
# ifdef ENABLE_MEMDBG
#  define ncmalloc(n, size)             xmalloc_dbg(n, size, __FILE__, __LINE__)
# else
#  define ncmalloc(n, size)             xmalloc(n, size)
# endif
# define ncrealloc(ptr, size, new_size) xrealloc(ptr, size, new_size)
# define ncfree(ptr, size)              xfree(ptr, size)
#endif

#define ncmem_usage()                   xmem_usage()


void *
xmalloc(size_t n, size_t size);

#if defined(PRINT_MEM) || defined(ENABLE_DEBUG)
void *
xmalloc_dbg(size_t n, size_t size, const char *file, const uint16_t line);
#endif

#ifdef _POSIX_MEMLOCK
void *
xmalloc_lock(size_t n, size_t size);
#else
# define xmalloc_lock(n, size) xmalloc(n, size)
#endif

void *
xrealloc(void *ptr, size_t size, size_t new_size);

void
xfree(void *ptr, size_t size);

void
xmem_usage(void);


/* memory-stats-per-request */
#ifdef ENABLE_REQDBG
void
xmemstat_print(int thrdnum, size_t reqnum);
#else
# define xmemstat_print(thrdnum, reqnum)
#endif

#endif
